<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 优惠卷管理
 * @author Mk
 */
class Coupon extends Admin_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model(array("Coupons_model","Coupon_info_model"));
	}
	function index($page_no=1){
		$page_no = max(intval($page_no),1);
		
		$where_arr = array();
		$orderby = $keyword= "";
		if (isset($_GET['dosubmit'])) {
			$keyword =isset($_GET['keyword'])?safe_replace(trim($_GET['keyword'])):'';
			if($keyword!="") $where_arr[] = "concat(coupon_name,actual_price) like '%{$keyword}%'";
			 
		}
		$where = implode(" and ",$where_arr);
		$data_list = $this->Coupons_model->listinfo($where,'*',$orderby , $page_no, $this->Coupons_model->page_size,'',$this->Coupons_model->page_size,page_list_url('adminpanel/Coupon/index',true));
		$this->view('index',array('data_list'=>$data_list,'pages'=>$this->Coupons_model->pages,'keyword'=>$keyword,'require_js'=>true));
		
	}
	function add(){
		//如果是AJAX请求
		if($this->input->is_ajax_request()){
			$type = trim($_POST["type"]);
			$fill_price = trim($_POST["fill_price"]);
			$start_time = trim($_POST["start_time"]);
			$end_time = trim($_POST["end_time"]);
			$coipon_name = trim($_POST["coupon_name"]);
			$actual_price = trim($_POST["actual_price"]);
			if($type != 1){
				if($start_time =="")exit(json_encode(array('status'=>false,'tips'=>'开始时间不能为空')));
				if($end_time =="")exit(json_encode(array('status'=>false,'tips'=>'结束时间不能为空')));
				if($fill_price =="")exit(json_encode(array('status'=>false,'tips'=>'满减价格不能为空')));
			}
			$data=array("type"=>$type,"coupon_name"=>$coipon_name,"start_time"=>$start_time,"end_time"=>$end_time,
					"actual_price"=>$actual_price,"fill_price"=>$fill_price,"creatime"=>date("Y-m-d H:i:s"),"createid"=>$_SESSION["user_id"]
			);
			$status = $this->Coupons_model->insert($data);
			if($status){
				exit(json_encode(array('status'=>true,'tips'=>'添加成功')));
			}else{
				exit(json_encode(array('status'=>false,'tips'=>'添加失败')));
			}
		}else{
			$this->view('add',array("is_edit"=>false,'require_js'=>true));
		}
	}
	//生成优惠卷数量
	public function edit($id=0){
		$id = intval($id);
		if($this->input->is_ajax_request()){
			$this->config->load("config");
			$image_url = $this->config->item("image_url");
			$flag = true;//标识是否完成优惠卷全部入库任务
			$count;
			$number = trim($_POST['number']);
			if($number =="")exit(json_encode(array('status'=>false,'tips'=>'生成优惠卷的个数不能为空')));
			//循环生成优惠卷
			for($i = 0;$i<$number;$i++){
				$rand = $this->getRandom();
				$value="http://www.baidu.com";
				$Qr="couponQrcode/".$rand.".png";
				$matrixPointSize=12;
				$errorCorrectionLevel="S";
				$this->load->library(array("CIQrcode"));
				$this->ciqrcode->generateQrcode($value,"./images/".$Qr,FALSE,$errorCorrectionLevel,$matrixPointSize,"");
				$data = array(
						"qrUrl"=>$image_url.$Qr,
						"act_code"=>$rand,
						"state"=>1,
						"couponid"=>$id,
						"creatime"=>date("Y-m-d H:i:s"),
						"creatid"=>$_SESSION["user_id"]
				);
				$status= $this->Coupon_info_model->insert($data);
				$count = $i+1;
				if(!$status){
					$flag = false;
					break;
				}
			}
			if($flag){
				exit(json_encode(array('status'=>$flag,'tips'=>"成功添加".$count."个优惠卷")));
			}else{
				exit(json_encode(array('status'=>$flag,'tips'=>'添加第'.$count."个优惠卷时失败")));
			}
		}else{
			$data_info['id'] = $id;
			$this->view('edit',array("is_edit"=>true,"data_info"=>$data_info,'require_js'=>true));
		}
	}
	//获取随机码
	private function getRandom(){
		$this->load->helper("qrcode");
		$radom = randStr(6);
		$flag = $this->Coupon_info_model->get_one(array("act_code"=>$radom));
		//保证随机码的唯一性
		if($flag){
			getRandom();
		}else{
			return $radom;
		}
	}
}
   